<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="/content/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
	String rootName = request.getParameter("rootName");
	String isNeedRoot = request.getParameter("isNeedRoot");
	String isNeedMenu = request.getParameter("isNeedMenu");
%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>货物类别管理</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="<%=basePath%>/js/jquery/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=basePath%>/js/jquery/themes/icon.css">
<script type="text/javascript" src="<%=basePath%>/js/jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=basePath%>/js/jquery/jquery.easyui.min.js"></script>
<script type="text/javascript">
	
		var isNeedRoot = <%=isNeedRoot!=null?isNeedRoot:"false"%>;
		var isNeedMenu = <%=isNeedMenu!=null?isNeedMenu:"false"%>;
		
		function loadTree(){
			var treeData = [];
			$.ajax({
			   type: "POST",
			   url: "<%=basePath%>content/common/typeTreeAction?rootName=<%=rootName%>",
			   async : false,
			   dataType : "json",
			   success: function(jsData){
			     if(isNeedRoot==true){
			     	treeData.push(jsData['tree']);
			     }else{
			     	treeData = jsData['tree']['children'];
			     }
			   }
			});
			$('#tree').tree({
				data: treeData,
				onClick:function(node){
					$(this).tree('toggle', node.target);
					nodeSelect(node);
				}
			});
		}
		
		var addDialogOpts = {
			modal:true,
			buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					var selectedNode = $('#tree').tree('getSelected');
					var parentId = selectedNode['id'];
					var name = $('#addForm').find("#name").val();
					var jsData = {
						"parentId":parentId,
						"name":name
					};
					
					$.ajax({
					   type: "POST",
					   url: "<%=basePath%>content/common/typeAction!add",
					   async : false,
					   data : jsData,
					   dataType : "text",
					   success: function(txt){
					     loadTree();
					     $('#addDialog').dialog('close');
					   }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#addDialog').dialog('close');
				}
			}]
		};
		
		function add(event){
			var e = event || window.event;
			var selectedNode = $('#tree').tree('getSelected');
			if(selectedNode == null){
				alert("请选择节点");
				return true;
			}
			$('#updateForm').find("#name").val("");
			$('#addDialog').attr("style","width:400px;height:200px;display:");
			$('#addDialog').dialog(addDialogOpts);
			$('#addDialog').dialog('open');
			$('#addDialog').dialog('move',{
			 	left:e.clientX,
			 	top:e.clientY
			 }); 
		}
		
		
		var updateDialogOpts = {
			modal:true,
			buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					var selectedNode = $('#tree').tree('getSelected');
					var id = selectedNode['id'];
					var name = $('#updateForm').find("#name").val();
					var parentId = selectedNode.attributes['parentId'];
					var jsData = {
						"parentId":parentId,
						"id":id,
						"name":name
					};
					
					$.ajax({
					   type: "POST",
					   url: "<%=basePath%>content/common/typeAction!update",
					   async : false,
					   data : jsData,
					   dataType : "text",
					   success: function(txt){
					     loadTree();
					     $('#updateDialog').dialog('close');
					   }
					});
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#updateDialog').dialog('close');
				}
			}]
		};
		
		function update(event){
			var e = event || window.event;
			
			var selectedNode = $('#tree').tree('getSelected');
			if(selectedNode == null){
				alert("请选择节点");
				return true;
			}
			$('#updateForm').find("#name").val(selectedNode.attributes['name']);
			
			$('#updateDialog').attr("style","width:400px;height:200px;display:");
			$('#updateDialog').dialog(updateDialogOpts);
			$('#updateDialog').dialog('open');
			$('#updateDialog').dialog('move',{
			 	left:e.clientX,
			 	top:e.clientY
			 });
		}
		
		function del(event){
			var e = event || window.event;
			var selectedNode = $('#tree').tree('getSelected');
			if(selectedNode == null){
				alert("请选择节点");
				return true;
			}
			var jsData = {
				"id":selectedNode['id']
			};
			
			$.ajax({
			   type: "POST",
			   url: "<%=basePath%>content/common/typeAction!del",
			   async : false,
			   data : jsData,
			   dataType : "text",
			   success: function(txt){
			     loadTree();
			   }
			});
		}
		
		function nodeSelect(node){
			var id=node.id;
			var icon=node.iconCls;
			var text=node.text;
			if("tree-folder"==icon){
				$("#select_id").val(id);
			}
			if("tree-file"==icon){
				$("#select_id").val(id);
				$("#select_text").val(text);
			}
		}
		$(function(){
			loadTree();
		});
	</script>
</head>
<body style="padding:0px; height:0px;">
<input name="id" type="hidden" value="" id="select_id">
<input name="text" type="hidden" value="" id="select_text">
<div id="appendDiv"></div>
<div id="treeDiv" style="position:fixed;left:10px;top:10px; width:340px; height:400px; overflow:auto;">
  <ul id="tree">
  </ul>
</div>
</body>
</html>
